import 'package:flutter/material.dart';
import '../tool/assets.dart';
import '../tool/tool.dart';
import '../class/connect.dart';
import '../class/update.dart';
import 'log.dart';

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});
  final String title;
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _currentIndex = 0;

  ///BottomNavigationBarItem的响应方法
  void _onTapHandler(int index) {
    setState(() {
      _currentIndex = index;
    });
  }

  ///使用selectedItemColor｜unselectedItemColor来渲染图片
  // ignore: unused_element
  Widget _renderItem1(BuildContext context) {
    return _currentIndex == 0
        ? const ImageIcon(AssetImage(assets.connect_select), size: 40)
        : const ImageIcon(AssetImage(assets.connect_unselect), size: 40);
  }

  ///使用selectedItemColor｜unselectedItemColor来渲染图片
  // ignore: unused_element
  Widget _renderItem2(BuildContext context) {
    return _currentIndex == 1
        ? const ImageIcon(AssetImage(assets.update_select), size: 40)
        : const ImageIcon(AssetImage(assets.update_unselect), size: 40);
  }

  ///使用图片的原色，而不用selectedItemColor｜unselectedItemColor来渲染
  Widget _originItem1(BuildContext context) {
    return Image.asset(
      _currentIndex == 0 ? assets.connect_select : assets.connect_unselect,
      width: 40,
      height: 40,
    );
  }

  ///使用图片的原色，而不用selectedItemColor｜unselectedItemColor来渲染
  Widget _originItem2(BuildContext context) {
    return Image.asset(
      _currentIndex == 1 ? assets.update_select : assets.update_unselect,
      width: 40,
      height: 40,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: BG_GREY,
      appBar: AppBar(
        leading: Container(),
        automaticallyImplyLeading: false, // 隐藏默认导航图标
        title: Text(widget.title),
        centerTitle: true,
      ),
      body: IndexedStack(
        index: _currentIndex,
        children: const [
          ConnectView(),
          UpdateView(),
        ],
      ),
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: _currentIndex,
        onTap: _onTapHandler,
        type: BottomNavigationBarType.fixed,
        //fixedColor: const Color(0x00436FEB),
        selectedItemColor: const Color(0xFF436FEB),
        unselectedItemColor: Colors.grey,
        showSelectedLabels: false,
        showUnselectedLabels: false,
        items: [
          BottomNavigationBarItem(
            icon: _originItem1(context),
            label: 'Connect',
          ),
          BottomNavigationBarItem(
            icon: _originItem2(context),
            label: 'Update',
          )
        ],
      ),
      drawer: const LogView(),
      drawerEdgeDragWidth: MediaQuery.of(context).size.width * 0.2,

      // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}
